package com.ui;

import com.service.GroupService;
import com.service.StudentService;
import com.model.Group;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class AddStudentDialog extends JDialog {
    private JComboBox<Group> groupComboBox;
    private JTextField studentIdField;
    private JTextField studentNameField;
    private JButton addButton;
    private JButton backButton;
    private StudentService studentService;
    private String currentClass;
    private GroupService groupService;
    private List<Group> groups; // 确保这个变量被定义

    public AddStudentDialog(StudentService studentService, GroupService groupService, String currentClass, List<Group> groups) {
        this.studentService = studentService;
        this.groupService = groupService;
        this.currentClass = currentClass;
        this.groups = groups; // 确保 groups 被正确赋值

  
        System.out.println("传入的小组列表: " + groups); // 确保这里打印出小组列表
        System.out.println("this.groups: " + this.groups); // 确认 this.groups 的值

        setTitle("添加学生");
        setSize(300, 200);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5); // 设置组件间距

        groupComboBox = new JComboBox<>();
        studentIdField = new JTextField(10);
        studentNameField = new JTextField(10);
        addButton = new JButton("添加");
        backButton = new JButton("返回");

        // 确保groups不是null且不为空
        if (this.groups != null && !this.groups.isEmpty()) {
            groupComboBox.setModel(new DefaultComboBoxModel<>(this.groups.toArray(new Group[0])));
        } else {
            JOptionPane.showMessageDialog(this, "当前班级下没有小组", "错误", JOptionPane.ERROR_MESSAGE);
            dispose(); // 关闭对话框，因为没有小组可供选择
        }

        // 小组下拉列表
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        panel.add(new JLabel("小组:"), gbc);
        gbc.gridx = 2;
        panel.add(groupComboBox, gbc);

        // 学号输入框
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        panel.add(new JLabel("学号:"), gbc);
        gbc.gridx = 2;
        panel.add(studentIdField, gbc);

        // 姓名输入框
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        panel.add(new JLabel("姓名:"), gbc);
        gbc.gridx = 2;
        panel.add(studentNameField, gbc);

        // 添加、返回按钮
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER; // 居中对齐
        panel.add(addButton, gbc);
        gbc.gridx = 2;
        panel.add(backButton, gbc);

        add(panel);

        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String studentIdString = studentIdField.getText().trim(); // 获取学号字符串
                String studentName = studentNameField.getText().trim();
                Group selectedGroup = (Group) groupComboBox.getSelectedItem();

                if (selectedGroup != null && !studentIdString.isEmpty() && !studentName.isEmpty()) {
                    try {
                        int studentId = Integer.parseInt(studentIdString); // 将学号字符串转换为整数
                        int selectedGroupId = selectedGroup.getId();
                        // 调用 studentService 的方法将学生信息存储到数据库中
                        studentService.addStudent(studentId, studentName, selectedGroupId);
                        JOptionPane.showMessageDialog(AddStudentDialog.this, "学生添加成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                        dispose(); // 关闭对话框
                    } catch (NumberFormatException ex) {
                        JOptionPane.showMessageDialog(AddStudentDialog.this, "学号必须是一个整数", "错误", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(AddStudentDialog.this, "请填写所有信息", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

}